package com.librelato.bean;

import java.util.HashMap;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.librelato.persistencia.SolicitacaoManutencaoDao;

@FacesConverter(value = "tipoServicoConverter", forClass = TipoServicoManutencaoBean.class)
public class TipoServicoManutencaoConverter implements Converter {

	private SolicitacaoManutencaoDao<SolicitacaoManutencaoBean> dao = new SolicitacaoManutencaoDao<SolicitacaoManutencaoBean>();

	private Map<String, String> param = new HashMap<String, String>();

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		TipoServicoManutencaoBean tipoServico = null;
		if (value.trim().equals("")) {
			return null;
		} else {
			try {
				if (value.trim().length() == 1) {
					value = "00000" + value;
				}
				param.put("CODTIPO", value);
				tipoServico = (TipoServicoManutencaoBean) dao
						.buscaTiposServico(param).get(0);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return tipoServico;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		if (value == null || value.equals("")) {
			return "";
		} else {
			return String.valueOf(((TipoServicoManutencaoBean) value)
					.getCodigo());
		}

	}

}
